package com.xinhua.erp.service;


import com.xinhua.erp.pojo.PageResult;
import com.xinhua.erp.pojo.ProcessBom;
import com.xinhua.erp.pojo.Result;
import com.xinhua.erp.repo.ProcessBomRepo;
import com.xinhua.erp.utils.IdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional

public class ProcessBomService {

    @Autowired
    private ProcessBomRepo processBomRepo;

    @Autowired
    private IdWorker idWorker;

    public Result findById(long id){

        return Result.OK(processBomRepo.getOne(id));

    }

    public Result findAllBom(){

        List<ProcessBom> list = processBomRepo.findAll();

        return Result.OK(list);
    }

    public Result findAll(int page,int size){

        Pageable pageable= PageRequest.of(page - 1, size);

        Page<ProcessBom> processBoms = processBomRepo.findAll(pageable);

        return Result.OK(new PageResult<>(processBoms.getTotalPages(),processBoms.getContent()));

    }

    public Result deleteById(long id){

        processBomRepo.deleteById(id);

        return Result.OK();

    }

    public Result save(ProcessBom processBom){

        processBomRepo.save(processBom);

        return Result.OK();

    }

    public Result update(ProcessBom processBom,long id){

        processBom.setId(id);

        processBomRepo.save(processBom);

        return Result.OK();

    }
    public Result Search(Map searchMap, int page, int size){
        Pageable pageable= PageRequest.of(page-1, size);
        Page<ProcessBom> processBoms= processBomRepo.findAll(createSpecification(searchMap),pageable);
        return Result.OK(new PageResult<>(processBoms.getTotalElements(),processBoms.getContent()));
    }

    private Specification<ProcessBom> createSpecification(Map searchMap){
        return new Specification<ProcessBom>() {
            @Override
            public Predicate toPredicate(Root<ProcessBom> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                ArrayList<Predicate> predicateList=new ArrayList<>();
                if(!StringUtils.isBlank((String)searchMap.get("uName"))){

                    predicateList.add(criteriaBuilder.like(root.get("uName").as(String.class),"%"+searchMap.get("uName")+"%"));

                }
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


}
